package com.rong.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rong.ruiji.common.R;
import com.rong.ruiji.dto.SetmealDto;
import com.rong.ruiji.entity.Dish;
import com.rong.ruiji.entity.SetMeal;
import com.rong.ruiji.service.CategoryService;
import com.rong.ruiji.service.SetMealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    @Autowired
    private SetMealService setMealService ;
    @Autowired
    private CategoryService categoryService ;

    /**
     * 新增套餐
     * @param request
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setMealCache",allEntries = true)
    public R<String> save(HttpServletRequest request, @RequestBody SetmealDto setmealDto) {
         Long emp = (Long)request.getSession().getAttribute("employee");
         setMealService.saveWithSetmealDish(emp,setmealDto);
         return R.success("新增成功");
    }

    /**
     * 获取套餐分页数据
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> get(@RequestParam int page,@RequestParam int pageSize,@RequestParam(required = false) String name) {
        Page<SetMeal> pageInfo = new Page<>();
        Page<SetmealDto> dtoPage = new Page<>();
        LambdaQueryWrapper<SetMeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(name != null,SetMeal::getName,name);
        queryWrapper.orderByDesc(SetMeal::getId);
        setMealService.page(pageInfo,queryWrapper);
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<SetMeal> list = pageInfo.getRecords();
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        for (SetMeal setMeal : list) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setMeal,setmealDto);
            String categoryName = categoryService.getById(setmealDto.getCategoryId()).getName();
            setmealDto.setCategoryName(categoryName);
            setmealDtoList.add(setmealDto);
        }
        dtoPage.setRecords(setmealDtoList);
        return R.success(dtoPage);
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setMealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<SetMeal>> list(SetMeal setmeal){
        LambdaQueryWrapper<SetMeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,SetMeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,SetMeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(SetMeal::getUpdateTime);

        List<SetMeal> list = setMealService.list(queryWrapper);

        return R.success(list);
    }

}
